//
//  UIColor+DDAdd.m
//  AFNetworking
//
//  Created by 许龙 on 2019/9/24.
//

#import "UIColor+DDAdd.h"
#import "NSString+DDAdd.h"

@implementation UIColor (DDAdd)

static inline NSUInteger hexStrToInt(NSString *str) {
    uint32_t result = 0;
    sscanf([str UTF8String], "%X", &result);
    return result;
}

static BOOL hexStrToRGBA(NSString *str, CGFloat *r, CGFloat *g, CGFloat *b, CGFloat *a) {
    str = [[str stringByTrim] uppercaseString];
    if ([str hasPrefix:@"#"]) {
        str = [str substringFromIndex:1];
    } else if ([str hasPrefix:@"0X"]) {
        str = [str substringFromIndex:2];
    }
    
    NSUInteger length = str.length;
    // RGB RGBA RRGGBB RRGGBBAA
    if (length != 3 && length != 4 && length != 6 && length != 8) {
        return NO;
    }
    
    if (length < 5) {
        *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 1)]) / 255.0f;
        *g = hexStrToInt([str substringWithRange:NSMakeRange(1, 1)]) / 255.0f;
        *b = hexStrToInt([str substringWithRange:NSMakeRange(2, 1)]) / 255.0f;
        if (length == 4) *a = hexStrToInt([str substringWithRange:NSMakeRange(3, 1)]) / 255.0f;
        else *a = 1;
    } else {
        *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 2)]) / 255.0f;
        *g = hexStrToInt([str substringWithRange:NSMakeRange(2, 2)]) / 255.0f;
        *b = hexStrToInt([str substringWithRange:NSMakeRange(4, 2)]) / 255.0f;
        if (length == 8) *a = hexStrToInt([str substringWithRange:NSMakeRange(6, 2)]) / 255.0f;
        else *a = 1;
    }
    return YES;
}

static BOOL hexStrToRGB(NSString *str, CGFloat *r, CGFloat *g, CGFloat *b) {
    str = [[str stringByTrim] uppercaseString];
    if ([str hasPrefix:@"#"]) {
        str = [str substringFromIndex:1];
    } else if ([str hasPrefix:@"0X"]) {
        str = [str substringFromIndex:2];
    }
    
    NSUInteger length = str.length;
    // RGB  RRGGBB
    if (length != 3 && length != 6) {
        return NO;
    }
    
    if (length == 3) {
        *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 1)]) / 255.0f;
        *g = hexStrToInt([str substringWithRange:NSMakeRange(1, 1)]) / 255.0f;
        *b = hexStrToInt([str substringWithRange:NSMakeRange(2, 1)]) / 255.0f;
    } else {
        *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 2)]) / 255.0f;
        *g = hexStrToInt([str substringWithRange:NSMakeRange(2, 2)]) / 255.0f;
        *b = hexStrToInt([str substringWithRange:NSMakeRange(4, 2)]) / 255.0f;
    }
    return YES;
}

+ (UIColor *)colorWithHexString:(NSString *)hexStr {
    CGFloat r, g, b, a;
    if (hexStrToRGBA(hexStr, &r, &g, &b, &a)) {
        return [UIColor colorWithRed:r green:g blue:b alpha:a];
    }
    return nil;
}

+ (UIColor *)colorWithHexString:(NSString *)hexStr alpha:(CGFloat)alpha {
    CGFloat r, g, b;
    if (hexStrToRGB(hexStr, &r, &g, &b)) {
        return [UIColor colorWithRed:r green:g blue:b alpha:alpha];
    }
    return nil;
}

+ (UIColor *)colorWithRGBA:(uint32_t)rgbaValue {
    return [UIColor colorWithRed:((rgbaValue & 0xFF000000) >> 24) / 255.0f
                           green:((rgbaValue & 0xFF0000) >> 16) / 255.0f
                            blue:((rgbaValue & 0xFF00) >> 8) / 255.0f
                           alpha:(rgbaValue & 0xFF) / 255.0f];
}

+ (UIColor *)colorByStartColor:(UIColor *)s endColor:(UIColor *)e percent:(CGFloat)percent {
    uint32_t s_rgba = [s rgbaValue];
    uint32_t e_rgba = [e rgbaValue];
    
    CGFloat sr = ((s_rgba & 0xFF000000) >> 24) / 255.0f;
    CGFloat sg = ((s_rgba & 0xFF0000) >> 16) / 255.0f;
    CGFloat sb = ((s_rgba & 0xFF00) >> 8) / 255.0f;
    CGFloat sa = (s_rgba & 0xFF) / 255.0f;
    CGFloat er = ((e_rgba & 0xFF000000) >> 24) / 255.0f;
    CGFloat eg = ((e_rgba & 0xFF0000) >> 16) / 255.0f;
    CGFloat eb = ((e_rgba & 0xFF00) >> 8) / 255.0f;
    CGFloat ea = (e_rgba & 0xFF) / 255.0f;
    // (end rgba value - start rgba value) * percent + start rgba value
    return [UIColor colorWithRed:(er - sr) * percent + sr
                           green:(eg - sg) * percent + sg
                            blue:(eb - sb) * percent + sb
                           alpha:(ea - sa) * percent + sa];
    
}

- (uint32_t)rgbaValue {
    CGFloat r = 0, g = 0, b = 0, a = 0;
    [self getRed:&r green:&g blue:&b alpha:&a];
    int8_t red = r * 255;
    uint8_t green = g * 255;
    uint8_t blue = b * 255;
    uint8_t alpha = a * 255;
    return (red << 24) + (green << 16) + (blue << 8) + alpha;
}

@end
